<?php

namespace app\admin\services;

use app\common\model\Member;
use app\common\services\BaseService;
use app\common\util\DateTimeUtil;
use app\common\util\weixin\TemplateMessage;

class QuestionAnswerService extends BaseService
{
    public function saveModel($data, $isAdding = null)
    {
        if (empty($data['id'])) {
            die;
        }
        $data['answer_time'] = DateTimeUtil::now();
        $data['status'] = 1;
        $rt = parent::saveModel($data, $isAdding); // TODO: Change the autogenerated stub
        $rt[] = '回复成功';
        $entry = $this->getModelInstance()->where(['id' => $data['id']])->find();
        //ahLPBpnufzDY1UbL_WLlyrgLW5-HgkgLkXHt9_FQnfU
        if (!empty($entry['member_id'])) {
            $touser = Member::instance()->where(['id' => $entry['member_id']])->getField('openid');
            if (!empty($touser)) {
                TemplateMessage::sendTemplateMessage([
                    'first' => ['value' => '你好，你的疑问已得到反馈'],
                    'keyword1' => ['value' => trim($entry['question'])],
                    'keyword2' => ['value' => trim($data['answer'])],
                    'keyword3' => ['value' => $data['answer_time']],
                    'remark' => ['value' => "\n" . '感谢您的参与', 'color' => '#173177']
                ], $touser, 'ahLPBpnufzDY1UbL_WLlyrgLW5-HgkgLkXHt9_FQnfU',
                    U('weixin/QuestionAnswer/detail', ['id' => $data['id']], true, true));
            }
        }

        return $rt;
    }
}